﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SystemCenterSoa.ApiProject.Area.SystemManager;
using SystemCenterSoa.DtoModel.Common;
using SystemCenterSoa.DtoModel.SystemSettings.InstituTions;
using SystemCenterSoa.PoModel.SystemSettings.InstituTions;
using SystemCenterSoa.Service.SystemSettings.InstituTions;

namespace SystemCenterSoa.ApiProject.Area.SystemSettings.InstituTions
{
    [AllowAnonymous]
    public class DepartmentManagerController : SystemManagerBaseController
    {
        private DepartmentService _departmentService;
        public DepartmentManagerController(DepartmentService departmentService)
        {
            _departmentService = departmentService;
        }
        [HttpGet]
        public ResultModel<List<DepartmentOfTypePo>> BindDepType()
        {
            try
            {
                var data = _departmentService.BindDepType();
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<List<DepartmentOfTypePo>>(ex);
            }
        }
        [HttpPost]
        public ResultModel<PagerResult<DepartmentOfManagementDto>> SearchPage(SearchModel sm)
        {
            try
            {
                var data = _departmentService.SearchPage(sm);
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<PagerResult<DepartmentOfManagementDto>>(ex);
            }


        }
        [HttpPost]
        public ResultModel<bool> DepartmentAdd(DepartmentOfManagementPo view)
        {
            try
            {
                view.creator_date = DateTime.Now;
                view.creator_name = LoginUser.user_full_name;
                var data = _departmentService.DepartmentAdd(view);
                if (data == false) //返回添加id为0,  说明添加没有成功, 直接返回失败
                {
                    return MyFail<bool>("添加没有成功!");
                }
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<bool>(ex);
            }
        }
        [HttpGet]
        public ResultModel<DepartmentOfManagementPo> GetDepartmentById(int id)
        {
            try
            {
                var data = _departmentService.GetDepartmentById(id);
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<DepartmentOfManagementPo>(ex);
            }
        }
        [HttpPost]
        public ResultModel<bool> DepartmentUpdate(DepartmentOfManagementPo view)
        {
            try
            {
                view.modifi_date = DateTime.Now;
                view.modifi_name = LoginUser.user_full_name;
                var data = _departmentService.DepartmentUpdate(view);
                if (data) //如果是成功就更新成功
                {
                    return MyOk(data);

                }
                return MyFail<bool>("更新没有成功!");

            }
            catch (Exception ex)
            {
                return MyError<bool>(ex);
            }
        }
        [HttpPost]
        public ResultModel<bool> DepartmentUpdateById(int id)
        {
            try
            {
                var data = _departmentService.DepartmentUpdateById(id);
                if (data) //如果是成功就更新成功
                {
                    return MyOk(data);

                }
                return MyFail<bool>("更新没有成功!");

            }
            catch (Exception ex)
            {
                return MyError<bool>(ex);
            }
        }
        [HttpPost]
        public ResultModel<bool> DepartmentDelete(string ids)
        {
            try
            {
                var data = _departmentService.DepartmentDelete(ids);
                if (data) //如果是成功就更新成功
                {
                    return MyOk(data);

                }
                return MyFail<bool>("删除没有成功!");

            }
            catch (Exception ex)
            {
                return MyError<bool>(ex);
            }
        }
    }
}
